/* JunCmd Version:0.7.2.3
   Bulid: 250315_-2
   Author:Ate_CaO2_Toothie
   WARNING: RunScript is not supported in AFile Version Not supported in This Edition
   Please using g++ 11.0.0 or higher to complied JunCmd
   Command(Linux): g++ JunCmd.cpp -o JunCmd.elf -std=c++11 
   Command(Windows): g++ JunCmd.cpp -o JunCmd.exe -std=c++11
   Please using g++ 11.0.0 or higher to complied AFile Version
   AFile Version: Not support Insider Edition
*/
#include "API.hpp"
#include <fstream>
#include <map>
#include <functional>
const std::string BuildVer =  "250412";
const std::string Version = "0.7.2.3";
const std::string VionInfo =  "Insider Edition";


class JunCmd {
public:
    std::string echo(std::string Message) {
        std::string Text = Message;
        std::cout << "\033[36m" << Text << "\033[0m" << std::endl;
        return "Outputed";
    }

    std::string help() {
        std::cout << "\033[32m" << "JunCmd Built-in Command: \"help\"" << std::endl;
        std::cout << "" << std::endl;
        std::cout << "\033[36m" << "clean Clean Console Screen" << std::endl;
        std::cout << "echo Text Output Text On the Console Screen" << std::endl;
        std::cout << "Easter-Eggs Easter Egg for JunCmd" << std::endl;
        std::cout << "\033[36m" << "exit/quit Exit JunCmd" << "\033[0m" << std::endl;
        std::cout << "\033[36m" << "prompt-ena Enable Prompt" << "\033[0m" << std::endl;
        std::cout << "\033[36m" << "prompt-dis Disable Prompt" << "\033[0m" << std::endl;
        std::cout << "\033[36m" << "prompt-mode Show Prompt Mode Status" << "\033[0m" << std::endl;
        std::cout << "\033[36m" << "JunCmd The Easter Eggs" << "\033[0m" << std::endl;
        std::cout << "\033[36m" << "VionInfo Show Vision Information" << "\033[0m" << std::endl;
        std::cout << "" << std::endl;
        std::cout << "\033[32m" << "help Show Offline(Local) Help Information" << "\033[0m" << std::endl;
        return "Command Executed";
    }

    std::string ShowError(std::string Error) {
        std::cout << "\033[31m" << Error << "\033[0m" << std::endl;
        return "";
    }
    
    std::string RunScript(std::string Script) {
        
        std::string ShellComm = "%JUNCMDROOT%\\RunScript.exe";
        std::string ScriptFile = ShellComm.substr(5);
        ScriptFile.erase(0, std::min(ScriptFile.find_first_not_of(' '), ScriptFile.size()));
        ShellComm.append(ScriptFile);
        ScriptFile.clear();
        system(ShellComm.c_str());
        return "Command Executed";
    }

    std::string RunToolkit(std::string ToolkitCommand) {
        // 直接 %JUNCMDROOT%\\Toolkit.exe
        std::string ShellComm = "%JUNCMDROOT%\\Toolkit.exe";
        std::string ToolkitCommandFile = ShellComm.substr(5);
        ToolkitCommandFile.erase(0, std::min(ToolkitCommandFile.find_first_not_of(' '), ToolkitCommandFile.size()));
        ShellComm.append(ToolkitCommandFile);
        ToolkitCommandFile.clear();
        system(ShellComm.c_str());
    }
    std::string VionInfo() {
        
        std::cout << "Version:" << Version << std::endl;
        std::cout << "Devloper:Ate_Ca_Toothie" << std::endl;
        std::cout << "You can visit space.bilibili.com/1873070088 to Watch My Videos,Dynamics and Article." << std::endl;
        std::cout << "Update Date:2024-4-21" << std::endl;
        std::cout << "Programming Language:Cpp" << std::endl;
        std::cout << "License:AGPLv3" << std::endl;
        std::cout << "Gitee:https://gitee.com/Toothie/JunCmd" << std::endl;
    }

};

int main(int argc, std::string argv[]) {
    JunCmd JunCmdClass;
    if (argc >= 2) {
        std::string Options = argv[1];
        if (Options == "--show-welcome" || Options == "-s") {
            std::cout << "\033[36m" << "JunCmd " << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "VisionInformation:[On Win64,Insider Edition,V0.7]" << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "Ate Ca Toothie's WARNING:JunCmd does not support Windows 7 and lower versions." << "\033[0m";
            std::cout << "\033[36m" << "Please use Windows 8 or higher to run JunCmd, as Windows 7 may have security vulnerabilities." << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "JunCmd does not support x86 architecture." << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "JunCmd is the Open Source Project" << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "You can visit https://juncmd.toothie.club for more information about JunCmd." << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "Try using \"help\" to get offline help information." << "\033[0m" << std::endl;
            std::cout << "" << std::endl;
        } else if (Options == "--show-help" || Options == "-h") {
            JunCmdClass.help();
            return 0;
        } else if (Options == "--show-version" || Options == "-v") {
            std::cout << "\033[36m" << "JunCmd Version:0.7.2.3" << "\033[0m" << std::endl;
            std::cout << "\033[36m" << "Bulid" << BuildVer << "\033[0m" << std::endl;
            std::cout << "You are so lazy!" << std::endl;
            std::cout << "Try JunCmd -c VionInfo" << std::endl;
            return 2;
        } else if (Options == "--commands" || Options == "-c") {
            if (argc >= 3) {
                std::string Command = argv[2];
                if (Command == "VionInfo") {
                    JunCmdClass.VionInfo();
                }
            }
        }
    }
    int PromptEm = true;
    std::string WorkDirectory = "D:\\Toothie-sFiles\\JunCmd\\Projects";

    // 使用std::map来存储命令与对应的处理函数
    std::map<std::string, std::function<void()>> commands = {
        {"exit", [&]() { exit(0); }},
        {"quit", [&]() { exit(0); }},
        {"help", [&]() { JunCmdClass.help(); }},
        {"prompt-mode", [&]() {
            if (PromptEm) {
                std::cout << "\033[36m" << "Prompt Mode is Enabled,Try \"Prompt-Dis\" to Disable Prompt." << "\033[0m" << std::endl;
            } else {
                std::cout << "\033[36m" << "Prompt Mode is Disabled,Try \"Prompt-Ena\" to Enable Prompt." << "\033[0m" << std::endl;
            }
        }},
        {"clean", [&]() {
            std::cout << "\033[2J" << std::endl;
        }},
        {"echo", [&]() {
        	std::string ShellComm = "%JUNCMDROOT%\\Interpret.exe";
            std::string outputText = ShellComm.substr(4);
            outputText.erase(0, std::min(outputText.find_first_not_of(' '), outputText.size()));
            JunCmdClass.echo(outputText);
        }},
        {"prompt-dis", [&]() {
            PromptEm = false;
            std::cout << "\033[32mDebug:Command Executed\033[0m" << std::endl;
        }},
        {"prompt-ena", [&]() {
            PromptEm = true;
            std::cout << "\033[32mDebug:Command Executed\033[0m" << std::endl;
        }},
        {"juncmd", [&]() {
            JunCmdClass.ShowError("FORK_TASK_ERROR");
            JunCmdClass.echo("Info:Easter Egg!");
        }},
        {"cd", [&]() { /* 开发者很懒 */ }},
        {"rusc", [&]() {
            
        	std::string ShellComm = "%JUNCMDROOT%\\RunScript.exe";
            std::string outputText = ShellComm.substr(4);
            outputText.erase(0, std::min(outputText.find_first_not_of(' '), outputText.size()));
            JunCmdClass.RunScript(outputText);
        }},
        {"114514", [&]() { std::cout << "Easter Egg ID:yalimasinei Info:Ah Ah Ah Ahhhhhhhhh!!!" << std::endl; }},
        {"1919810", [&]() { std::cout << "Easter Egg ID:yalimasinei Info:Ah Ah Ah Ahhhhhhhhh!!!" << std::endl; }},
        {"1145141919810", [&]() { std::cout << "Easter Egg ID:yalimasinei Info:Ah Ah Ah Ahhhhhhhhh!!!" << std::endl; }},
        {"VionInfo", [&]() {
            JunCmdClass.VionInfo();
        }},
        {"easter-eggs", [&]() {
            std::cout << "Easter Egg ID:yalimasinei Info:Ah Ah Ah Ahhhhhhhhh!!! Comm:114514/1919810..." << std::endl;
            std::cout << "Easter Egg ID:JunCmd Info:\033[31mError:FORK_TASK_ERROR\033[0m Comm:JunCmd" << std::endl;
            std::cout << "Easter Egg ID:self | slef Info:\033[31mError:PROGRAM_RAM_ERROR\033[0m Comm:$0 | $0" << std::endl;
        }},
        {"$0 | $0", [&]() { std::cout << "\033[31mError:PROGRAM_RAM_ERROR\033[0m" << std::endl; }},
        {"error", [&]() {
            JokingError();
            system("pause>nul");
        }}
    };

    while (true) {
        if (PromptEm) {
            std::cout << "JunCmd " << WorkDirectory << "> ";
        } else {
            std::cout << "> ";
        }

        std::string Comm;
        std::getline(std::cin, Comm);

        // 查找并执行命令
        auto it = commands.find(Comm);
        if (it != commands.end()) {
            it->second();
        }
    }
}
